package cn.edu.qzuie.artist.module.td.controller.admin.activity;

import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.activity.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.activity.ActivityDO;
import cn.edu.qzuie.artist.module.td.service.activity.ActivityService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - TD活动")
@RestController
@RequestMapping("/td/activity")
@Validated
public class ActivityController {

    @Resource
    private ActivityService activityService;

    @PostMapping("/create")
    @Operation(summary = "创建活动")
    @PreAuthorize("@ss.hasPermission('td:activity:create')")
    public CommonResult<Long> createActivity(@Valid @RequestBody ActivitySaveReqVO createReqVO) {
        return success(activityService.createActivity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动")
    @PreAuthorize("@ss.hasPermission('td:activity:update')")
    public CommonResult<Boolean> updateActivity(@Valid @RequestBody ActivitySaveReqVO updateReqVO) {
        activityService.updateActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:activity:delete')")
    public CommonResult<Boolean> deleteActivity(@RequestParam("id") Long id) {
        activityService.deleteActivity(id);
        return success(true);
    }

//    @GetMapping("/get")
//    @Operation(summary = "获得活动")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('td:activity:query')")
//    public CommonResult<ActivityRespVO> getActivity(@RequestParam("id") Long id) {
//        ActivityDO activity = activityService.getActivity(id);
//        return success(BeanUtils.toBean(activity, ActivityRespVO.class));
//    }

    /**
     * 多表查询
     * @param id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "获得活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:activity:query')")
    public CommonResult<ActivityRespVO> getActivity(@RequestParam("id") Long id) {
        ActivityRespVO activity = activityService.getActivitys(id);
        return success(activity);
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得活动分页")
//    @PreAuthorize("@ss.hasPermission('td:activity:query')")
//    public CommonResult<PageResult<ActivityRespVO>> getActivityPage(@Valid ActivityPageReqVO pageReqVO) {
//        PageResult<ActivityDO> pageResult = activityService.getActivityPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, ActivityRespVO.class));
//    }
    //ActivityPagesRespVO
    @GetMapping("/page")
    @Operation(summary = "获得活动分页")
    @PreAuthorize("@ss.hasPermission('td:activity:query')")
    public CommonResult<PageResult<ActivityPagesRespVO>> getActivityPage(@Valid ActivityPageReqVO pageReqVO) {
        PageResult<ActivityPagesRespVO> pageResult = activityService.getActivityPageWithName(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityPagesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动 Excel")
    @PreAuthorize("@ss.hasPermission('td:activity:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportActivityExcel(@Valid ActivityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityDO> list = activityService.getActivityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动.xls", "数据", ActivityRespVO.class,
                        BeanUtils.toBean(list, ActivityRespVO.class));
    }

    //更新活动状态
    @PostMapping("/activityState/{activityState}")
    @Operation(summary = "更新活动状态")
    @PreAuthorize("@ss.hasPermission('td:activity:update')")
    public CommonResult<Boolean> updateActivityState(Long id,@PathVariable("activityState") Integer activityState) {
        //更新操作
        activityService.updateActivityState(activityState, id);
        return success(true);
    }

}